package com.pi;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import pl.jsolve.templ4docx.core.Docx;
import pl.jsolve.templ4docx.core.VariablePattern;
import pl.jsolve.templ4docx.variable.TextVariable;
import pl.jsolve.templ4docx.variable.Variables;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Date;

/**
 * 生成文档
 */
public class DocPlugin {

    /**
     * 生成发布文档
     *
     * @param config
     * @param patcherName
     * @param lastLog
     * @param devName
     * @param testerName
     * @throws URISyntaxException
     */
    public static void gen(Config config, String patcherName, String lastLog, String devName, String testerName) throws URISyntaxException {

        URL url = DocPlugin.class.getClassLoader().getResource("prepublish.docx");

        String baseName = FilenameUtils.getBaseName(patcherName);

        FileUtil.createFileIfNotExists(new File(config.getDocOutput()));

        String fileName = config.getDocOutput() + baseName + "发布申请单.docx";

        Docx docx = new Docx(url.getPath());
        docx.setVariablePattern(new VariablePattern("#{", "}"));

        Variables variables = new Variables();
        variables.addTextVariable(new TextVariable("#{patcherName}", patcherName));
        variables.addTextVariable(new TextVariable("#{lastLog}", lastLog));
        variables.addTextVariable(new TextVariable("#{env}", "Update"));
        variables.addTextVariable(new TextVariable("#{devName}", devName));
        variables.addTextVariable(new TextVariable("#{testerName}", testerName));

        docx.fillTemplate(variables);

        docx.save(fileName);

    }

    /**
     * 生成测试文档
     *
     * @param config
     * @param patcherName
     * @param lastLog
     * @param devName
     * @param testerName
     */
    public static void genTestCase(Config config, String patcherName, String lastLog, String devName, String testerName) {

        URL url = DocPlugin.class.getClassLoader().getResource("test_apply.docx");

//        String baseName = FilenameUtils.getBaseName(patcherName);
        FileUtil.createFileIfNotExists(new File(config.getDocOutput()));

        String fileName = config.getDocOutput() + DateFormatUtils.format(new Date(), "yyyyMMddHH") + "提测申请单.docx";

        Docx docx = new Docx(url.getPath());
        docx.setVariablePattern(new VariablePattern("#{", "}"));

        Variables variables = new Variables();
        variables.addTextVariable(new TextVariable("#{patcherName}", patcherName));
        variables.addTextVariable(new TextVariable("#{lastLog}", lastLog));
        variables.addTextVariable(new TextVariable("#{devName}", devName));
        variables.addTextVariable(new TextVariable("#{testerName}", testerName));
        variables.addTextVariable(new TextVariable("#{DateTime}", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")));

        docx.fillTemplate(variables);

        docx.save(fileName);

    }
 
}
